package com.example.niuke.service;

import com.example.niuke.dao.MessageMapper;
import com.example.niuke.entity.Message;
import com.example.niuke.util.SensitiveFilter;
import org.aspectj.bridge.MessageWriter;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageService {
    /**
     * Auther sun
     * DATE 2022/4/17 16:02
     * VERSION 1.0
     */

    @Resource
    private MessageMapper messageMapper;
    @Resource
    private SensitiveFilter sensitiveFilter;

    public List<Message> findConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId,offset,limit);
    }
    public int findConversationCount(int userId){
        return messageMapper.selectConversationCount(userId);
    }
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset,limit);
    }
    public int findLetterCount(String conversationId) {
        return messageMapper.selectLettersCount(conversationId);
    }
    public int findLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }

    public int add(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    public int readMessage(List<Integer> ids) {
        return messageMapper.updateStatus(ids, 1);
    }

    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.selectLatestNotice(userId, topic);
    }
    public int finNoticeCount(int userId, String topic) {
        return messageMapper.selectNoticeCount(userId, topic);
    }
    public int findNoticeUnreadCount(int userId, String topic) {
        return messageMapper.selectNoticeUnReadCount(userId, topic);
    }

    public List<Message> findNotices(int userId, String topic, int offset, int limit ) {
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }
}
